﻿using SharpDX.DirectInput;

namespace TableTool
{
    public partial class ElsTool : Form
    {
        int[] fuben63 = new int[] { 3, 4, 19, 7, 8, 11, 12, 15, 16, 19, 20, 23, 24, };
        int[] fuben94 = new int[] { 1, 2, 5, 6, 9, 10, 13, 14, 17, 18, 21, 22, };
        Config config { get; set; }
        DateTime F1 { get; set; }
        DateTime F2 { get; set; }
        DateTime F3 { get; set; }
        DateTime F4 { get; set; }
        DateTime E { get; set; }
        bool flag {  get; set; }
        public ElsTool()
        {
            InitializeComponent();
            this.FormClosing += ElsTool_FormClosing;
            flag = true;
            F1 = DateTime.Now;
            F2 = DateTime.Now;
            F4 = DateTime.Now;
            F3 = DateTime.Now;
            E = DateTime.Now;
            config = Config.Get<Config>();
            var directInput = new DirectInput();
            var Devices = directInput.GetDevices(DeviceType.Keyboard, DeviceEnumerationFlags.AllDevices);
            foreach (var item in Devices)
            {
                if (item.Type == DeviceType.Keyboard)
                {
                    var mykey = new SharpDX.DirectInput.Keyboard(directInput);
                    mykey.Acquire();
                    Task.Factory.StartNew(() =>
                    {
                        while (flag)
                        {
                            KeyboardState kstate = mykey.GetCurrentState();
                            if (kstate != null)
                            {
                                string value = "";
                                foreach (var device in kstate.PressedKeys)
                                {
                                    if (device==Key.F1)
                                    {
                                        F1=DateTime.Now;
                                    }
                                    if (device == Key.F2)
                                    {
                                        F2 = DateTime.Now;
                                    }
                                    if (device == Key.D6)
                                    {
                                        F3 = DateTime.Now;
                                    }
                                    if (device == Key.E)
                                    {
                                        E = DateTime.Now;
                                    }
                                    value += device.ToString()+" ";
                                    //Console.WriteLine(device.ToString());
                                }
                                SetLable(label5, value);
                                Thread.Sleep(200);
                            }
                        }
                    });
                }
            }
        }

        private void ElsTool_FormClosing(object? sender, FormClosingEventArgs e)
        {
            flag = false;
            timer1.Stop();
        }

        private void SetLable(Label lb,string value)
        {
            if (lb.InvokeRequired)
            {
                lb.Invoke(new Action<Label,string>(SetLable), lb, value);
            }
            else
            {
                lb.Text= value;
            }
        }
        private void ElsTool_Load(object sender, EventArgs e)
        {
            tbEdBiLi.Text = config.edBiLi.ToString();
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (fuben63.Contains(DateTime.Now.Hour))
            {
                label1.Text = "当前开门63";
            }
            else
            {
                label1.Text = "当前开门94";
            }
            SetLable(label7, tbBaiGui, F1);

            SetLable(label8, tbJueXin, F2);

            SetLable(label10, tbDouzhi, F3);
          
            SetLable(label12, textBox1, E);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lb"></param>
        /// <param name="value"></param>
        /// <param name="max"></param>
        private void SetLable(Label lb, TextBox tb,DateTime dt)
        {
            int.TryParse(tb.Text, out int max);
            Double sp = (DateTime.Now - dt).TotalSeconds;
            if (sp >= max)
            {
                lb.ForeColor = Color.Green;
                lb.Text = "就绪";
            }
            else
            {
                lb.ForeColor = Color.Red;
                lb.Text = sp.ToString("f2");
            }
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            rbRmb.TextChanged -= textBox3_TextChanged;
            if (double.TryParse(tbEdBiLi.Text, out double bili))
            {
                if (double.TryParse(tbED.Text, out double ed))
                {
                    var dst = ed / 1 * bili;
                    SetBili(rbRmb, dst);
                }
            }
            rbRmb.TextChanged += textBox3_TextChanged;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            tbED.TextChanged -= textBox2_TextChanged;
            if (double.TryParse(tbEdBiLi.Text, out double bili))
            {
                if (double.TryParse(rbRmb.Text, out double rmb))
                {
                    var dst = rmb / bili * 1;
                    SetBili(tbED, dst);
                }
            }
            tbED.TextChanged += textBox2_TextChanged;
        }

        private void SetBili(TextBox textBox, double value)
        {
            textBox.Text = value.ToString();
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            Console.WriteLine(m);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            伤害计算 sb = new 伤害计算();
            sb.Show();
        }
    }
}
